<template>
  <div class="login-container">
    <div class="register">
      <router-link to="/login/agent">
        <h5 class="plant_register">后台登录</h5>
      </router-link>
    </div>

    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">厂家/商家 注册</h3>
      </div>

      <!--联系电话-->
      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="联系电话"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
          onkeyup="value=value.replace(/[^\d]/g,'')"
        />
      </el-form-item>
      <a class="acquire_note_code">
        <span ref="loginForm" :model="loginForm">获取短信验证码</span>
      </a>

      <!--手机验证码-->
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="手机验证码"
          name="code"
          type="text"
          tabindex="1"
          auto-complete="on"
          class="verify_code"
        />
        <span class="verify_coder">
          <el-button @click="sendCode(loginForm.mobile)" class="code_button">发送验证码</el-button>
        </span>
      </el-form-item>

      <!--联系人-->
      <el-form-item prop="real_name">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          ref="real_name"
          v-model="loginForm.real_name"
          placeholder="联系人"
          name="real_name"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <!--推荐人手机号-->
      <el-form-item prop="p_mobile">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          ref="p_mobile"
          v-model="loginForm.p_mobile"
          placeholder="推荐人手机号"
          name="p_mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
          onkeyup="value=value.replace(/[^\d]/g,'')"
        />
      </el-form-item>

      <!--业务类型-->
      <el-form-item prop="agent_type">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-select v-model="loginForm.agent_type" placeholder="请选择业务" style="width:346px">
          <el-option
            style="width:300px"
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <span class="verify_coder" style="margin-right: 40px;line-height:50px;">业务</span>
      </el-form-item>

      <!-- <el-form-item label="活动名称" prop="name">
        <el-input v-model="loginForm.agent_type"></el-input>
      </el-form-item>-->

      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >注册</el-button>

      <!--<div class="tips">-->
      <!--<span style="margin-right:20px;">username: admin</span>-->
      <!--<span> password: any</span>-->
      <!--</div>-->
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位"));
      } else {
        callback();
      }
    };

    const mobile = (rule, value, callback) => {
      if (!/^1[34578]\d{9}$/.test(value)) {
        callback(new Error("请输入正确的联系电话(不支持座机)"));
      } else {
        callback();
      }
    };

    return {
      loginForm: {
        real_name: "",
        p_mobile: "",
        mobile: "",
        code: "",
        agent_type: ""
      },
      loginRules: {
        real_name: [
          { required: true, trigger: "blur", message: "请输入联系人名字" }
        ],
        p_mobile: [{ required: true, trigger: "blur", validator: mobile }],
        mobile: [{ required: true, trigger: "blur", validator: mobile }],
        code: [
          { required: true, trigger: "blur", message: "请输入手机验证码" }
        ],
        agent_type: [
          { required: true, trigger: "blur", message: "请输选择一项业务" }
        ]
      },
      options: [{ id: 0, name: "厂家" }, { id: 1, name: "代理" }],
      loading: false,
      passwordType: "password",
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    sendCode(code) {
      if (!code || code == "" || code == null) {
        this.$message({
          message: "请填写联系电话",
          duration: 6000
        });
        return;
      }

      this.$http
        .post("/api/tool/ali/sms/vc", {
			mobile: this.loginForm.mobile
		})
        .then(response => {
          if (response.data.status == "success") {
            this.$message({
              message: "验证码已发送，请注意查收",
              duration: 6000
            });
          } else {
            this.$message({
              message: "添加失败，原因：" + response.data.failedMsg,
              duration: 6000
            });
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });

      console.log(code);
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (!valid) {
          return false;
        } else {
          this.loading = true;
          this.loginForm.company_id = 1;
          this.$http
            .post("/agent/register", this.loginForm)
            .then(response => {
              if (response.data.status == "success") {
                this.$message({
                  message: "注册成功",
                  duration: 6000
                });

                this.$router.push({
                  path: "/login"
                });
              } else {
                this.$message({
                  message: "添加失败，原因：" + response.data.failedMsg,
                  duration: 6000
                });
              }
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .register {
    position: absolute;
    right: 0;
    top: 0;
    display: flex;
    font-size: 20px;
    text-align: center;

    .plant_register {
      color: $light_gray;
      margin-right: 40px;
    }
    .dealer_register {
      color: $light_gray;
      margin-right: 40px;
    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 50px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .verify_code {
    width: 60%;
  }
  .verify_coder {
    float: right;
    height: 52px;
    background-color: #283443;
    color: white;
    font-size: normal;
    align-content: center;
    font-family: sans-serif;
    font-weight: 500;
  }
  .code_button {
    height: 52px;
    background: #283443;
    color: white;
  }
}
</style>
